package com.cdzy.controller;

import com.cdzy.common.ApiResponse;
import com.cdzy.pojo.Book;
import com.cdzy.pojo.BookType;
import com.cdzy.pojo.EntityVO;
import com.cdzy.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/book")
@CrossOrigin
public class BookController {
    @Autowired
    BookService bookService;

    // 获取书籍分类列表
    @GetMapping("/getBookTypeList")
    public ApiResponse<?> getBookTypeList() {
        List<BookType> bookTypes = bookService.getBookTypeList();
        return ApiResponse.success(bookTypes);
    }

    // 根据书籍类型查找书籍列表
    @GetMapping("/getBookListByType/{id}")
    public ApiResponse<?> getBookListByType(@PathVariable("id") int id) {
        List<Book> bookList = bookService.getBookListByType(id);
        return ApiResponse.success(bookList);
    }
    // 查找所有书籍
    @GetMapping("/getBookList")
    public ApiResponse<?> getBookListByType() {
        EntityVO<Book> bookList = bookService.getBookList();
        return ApiResponse.success(bookList);
    }
    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    public ResponseEntity<Book> queryById(@PathVariable("id") Integer id) {
        return ResponseEntity.ok(this.bookService.queryById(id));
    }

    /**
     * 新增数据
     *
     * @param books 实体
     * @return 新增结果
     */
    @PostMapping("/insert")
    public ResponseEntity<Book> add(@RequestBody Book books) {
        return ResponseEntity.ok(this.bookService.insert(books));
    }

    /**
     * 编辑数据
     *
     * @param books 实体
     * @return 编辑结果
     */
    @PutMapping("/edit")
    public ResponseEntity<Book> edit(@RequestBody Book books) {
        return ResponseEntity.ok(this.bookService.update(books));
    }

    /**
     * 通过id删除数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @DeleteMapping("/deleteById/{id}")
    public ResponseEntity<Boolean> deleteById(@PathVariable Integer id) {
        return ResponseEntity.ok(this.bookService.deleteById(id));
    }
}